package com.hq.shop05resurces.controller;


import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    @Value("${fdfs.nginxHome}")
    private String nginxHome;

    @PostMapping("upload")
    public String upload(MultipartFile file) throws IOException {

        InputStream ips = file.getInputStream();
        long size = file.getSize();
//        获取文件的后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(ips, size, extension, null);

        String fullPath = storePath.getFullPath();
        return nginxHome + fullPath;
    }
}
